package com.topscomm.main.config;

import com.topscomm.basic.util.Config;
import com.topscomm.cbo.service.ICboMessageAppService;
import com.topscomm.cbo.service.ICboMessageSysService;
import com.topscomm.cbo.service.impl.CboMessageAppService;
import com.topscomm.ltc.service.ILtcChanceBackboneService;
import com.topscomm.mis.mobile.message.service.CboMessageAppRewriteService;
import com.topscomm.mis.mobile.message.service.CboMessageSysRewriteService;
import com.topscomm.mms.service.IMmsDeptExtraInfoService;
import com.topscomm.tap.service.ITapPasswordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;

import static com.topscomm.mis.constant.MisSystemConst.PushMobileMessageMode.*;

@Configuration
public class HessianConfig {

	private static final Logger log= LoggerFactory.getLogger(HessianConfig.class);

	// LTC
	@Bean
	@Primary
	public HessianProxyFactoryBean ltcChanceBackboneService(){
		HessianProxyFactoryBean factoryBean = new HessianProxyFactoryBean();
		factoryBean.setConnectTimeout(3000);
		factoryBean.setReadTimeout(10000);
		factoryBean.setOverloadEnabled(true);
		factoryBean.setServiceUrl(Config.getInstance().getValue("LtcServerUrl", "") + "/ltcHessian/ltcChanceBackboneService");
		factoryBean.setServiceInterface(ILtcChanceBackboneService.class);
		return factoryBean;
	}
	//BSP
	@Bean
	@Primary
	public HessianProxyFactoryBean mmsDeptExtraInfoService(){
		HessianProxyFactoryBean factoryBean = new HessianProxyFactoryBean();
		factoryBean.setConnectTimeout(3000);
		factoryBean.setReadTimeout(200000);
		factoryBean.setOverloadEnabled(true);
		factoryBean.setServiceUrl(Config.getInstance().getValue("BspServerUrl", "") + "/bspHessian/mmsDeptExtraInfoService");
		factoryBean.setServiceInterface(IMmsDeptExtraInfoService.class);
		return factoryBean;
	}
	@Bean
	@Primary
	public HessianProxyFactoryBean cboUserPwdService() {
		HessianProxyFactoryBean factoryBean = new HessianProxyFactoryBean();
		factoryBean.setConnectTimeout(3000);
		factoryBean.setReadTimeout(10000);
		factoryBean.setOverloadEnabled(true);
		factoryBean.setUsername("admin");
		factoryBean.setPassword("admin");
		factoryBean.setServiceUrl(Config.getInstance().getValue("TopscommUipUrl", "") + "/misHessian/cboUserPwdService");
		factoryBean.setServiceInterface(ITapPasswordService.class);
		return factoryBean;
	}
	@Bean
	@ConditionalOnProperty(value = "pushMobileMessageMode", havingValue = HESSIAN_MODE, matchIfMissing = true)
	public HessianProxyFactoryBean cboMessageAppService() {
		log.info("==========================实例化远程hessian消息推送========================");
		HessianProxyFactoryBean factoryBean = new HessianProxyFactoryBean();
		factoryBean.setConnectTimeout(3000);
		factoryBean.setReadTimeout(200000);
		factoryBean.setOverloadEnabled(true);
		factoryBean.setUsername("admin");
		factoryBean.setPassword("admin");
		factoryBean.setServiceUrl(Config.getInstance().getValue("TopscommServerUrl", "") + "/misHessian/cboMessageAppService");
		factoryBean.setServiceInterface(ICboMessageAppService.class);
		return factoryBean;
	}

	@Bean(name = "cboMessageAppService")
	@ConditionalOnProperty(value = "pushMobileMessageMode", havingValue = LOCAL_MODE, matchIfMissing = true)
	public ICboMessageAppService cboMessageAppServiceforLocal() {
		log.info("==========================实例化本地消息推送========================");
		return new CboMessageAppService();
	}

	@Bean(name = "cboMessageAppService")
	@Primary
	@ConditionalOnProperty(value = "pushMobileMessageMode", havingValue = MSG_BUS_MODE, matchIfMissing = true)
	public ICboMessageAppService getCboMessageAppRewriteService() {
		log.info("=========================实例化消息推送==================O.o");
		return new CboMessageAppRewriteService();
	}

	@Bean
	@ConditionalOnProperty(value = "pushMobileMessageMode", havingValue = HESSIAN_MODE, matchIfMissing = true)
	public HessianProxyFactoryBean cboMessageSysService() {
		HessianProxyFactoryBean factoryBean = new HessianProxyFactoryBean();
		factoryBean.setConnectTimeout(3000);
		factoryBean.setReadTimeout(100000);
		factoryBean.setOverloadEnabled(true);
		factoryBean.setUsername("admin");
		factoryBean.setPassword("admin");
		factoryBean.setServiceUrl(Config.getInstance().getValue("TopscommServerUrl", "") + "/misHessian/cboMessageSysService");
		factoryBean.setServiceInterface(ICboMessageSysService.class);
		return factoryBean;
	}

	@Bean(name = "cboMessageSysService")
	@Primary
	@ConditionalOnProperty(value = "pushMobileMessageMode", havingValue = MSG_BUS_MODE, matchIfMissing = true)
	public ICboMessageSysService getCboMessageSysRewriteService() {
		return new CboMessageSysRewriteService();
	}
}
